package erp.server.role.service.impl;

import erp.server.base.enums.RoleLevelCode;
import erp.server.base.enums.RoleStatusCode;
import erp.server.organization.mapper.OrganizationMapper;
import erp.server.role.mapper.RoleMapper;
import erp.server.role.pojo.dto.RoleAddParam;
import erp.server.role.pojo.dto.RoleQueryParam;
import erp.server.role.pojo.dto.RoleUpdateParam;
import erp.server.role.pojo.entity.Role;
import erp.server.role.pojo.vo.RoleVO;
import erp.server.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private OrganizationMapper organizationMapper;

    //加入组织
    @Override
    public void insert(RoleAddParam roleAddParam) {
        log.debug("roleAddParam:{}",roleAddParam);
        Role role = new Role();
        BeanUtils.copyProperties(roleAddParam, role);
        //设置默认角色
        if (role.getLevel() == null){
            role.setLevel(RoleLevelCode.LEVEL_USER.getCode());
        }
        if (role.getStatus() == null){
            role.setStatus(RoleStatusCode.PENDING_REVIEW.getCode());
        }
        roleMapper.insert(role);
    }

    //删除组织
    @Override
    public void delete(Integer id) {
        log.debug("id-service:{}",id);
        roleMapper.deleteByOrganizationId(id);
    }

    //查询加入组织的成员
    @Override
    public List<RoleVO> select(RoleQueryParam roleQueryParam) {
        log.debug("roleQueryParam:{}",roleQueryParam);
        List<RoleVO> roleVOS = roleMapper.select(roleQueryParam);
        return roleVOS;
    }

    //修改人员信息
    @Override
    public void update(RoleUpdateParam roleUpdateParam) {
        log.debug("roleUpdate:{}",roleUpdateParam);
        roleMapper.updateByUserId(roleUpdateParam);
    }

    //删除用户
    @Override
    public void deleteUser(Integer userId, Integer orgId) {
        log.debug("userID:{}",userId);
        log.debug("orgID:{}", orgId);
        roleMapper.deleteById(userId, orgId);
    }

    @Override
    public void updateStatus(RoleUpdateParam roleUpdateParam) {
        log.debug("修改状态-入参:roleUpdateParam:{}",roleUpdateParam);
        roleMapper.updateStatusById(roleUpdateParam.getId(),roleUpdateParam.getStatus());
    }

    @Override
    public int countEmployees(Integer orgId, String status) {
        return roleMapper.selectCountEmployees(orgId,status);
    }


}
